Option Explicit On
Imports System.IO
Imports System.Runtime.InteropServices
''' <summary>
''' progress class for processing htm files
''' </summary>
''' <remarks>does not have yet the Total number of htm files</remarks>
Public Class ProcessFileEventArgs
    Inherits EventArgs

    ''' <summary>
    ''' name of the processed htm file
    ''' </summary>
    Public FileNameProcessed As String
    ''' <summary>
    ''' the number of the file processed
    ''' </summary>
    Public FileNumberProcessed As Long


    Friend Sub New(ByVal FileName As String, ByVal FileNumber As Long)
        FileNameProcessed = FileName
        FileNumberProcessed = FileNumber


    End Sub
End Class

''' <summary>
''' transforms multiple htm files
''' as been into hhc file into word
''' </summary>
''' <remarks>Makes use of WORD!</remarks>
Public Class clsWord
    ''' <summary>
    ''' event for having some feedback about processing
    ''' </summary>    
    Public Event ProcessFile As EventHandler(Of ProcessFileEventArgs)

    ''' <summary>
    ''' gets the word object
    ''' </summary>
    ''' <returns>Word application</returns>
    ''' <remarks>Must have word on the PC and rights to instantiate</remarks>
    Private Function Word() As Object
        Try
            Word = CreateObject("Word.Application")
        Catch ex As Exception
            Word = Nothing
        End Try


    End Function
    ''' <summary>
    ''' main function - parses the hhc files and finds htm 
    ''' and inserts into new word document
    ''' </summary>
    ''' <param name="pathFileHHC">name of the HHC file</param>
    ''' <param name="saveAs">Word file name to save </param>
    ''' <remarks>makes use of Word</remarks>
    Public Sub AddToWord(ByVal pathFileHHC As String, ByVal saveAs As String)

        If File.Exists(saveAs) Then
            Throw (New clsError("File " & saveAs & " exist", ErrorsOcurred.WordFileExist))
        End If

        Dim wApp As Object = Word()
        If (wApp Is Nothing) Then
            Throw (New clsError("Word does not exist", ErrorsOcurred.WordNotFound))
        End If
        Dim wDoc As Object = Nothing
        Try


            wDoc = wApp.Documents.Add
            wDoc.SaveAs(saveAs)

            Dim dirfile As String

            dirfile = Path.GetDirectoryName(pathFileHHC)
            Dim lines As String() = File.ReadAllLines(pathFileHHC)

            Dim quote As String = Chr(34)
            Dim filenumber As String = 0
            For Each TextLine As String In lines
                Dim htmFile As String
                If (InStr(1, TextLine, ".htm", vbTextCompare)) > 0 Then
                    Dim endQuote As Integer
                    endQuote = InStr(InStr(1, TextLine, ".htm", vbTextCompare), TextLine, quote)
                    Dim quoteLoop As Integer
                    quoteLoop = endQuote - 1
                    While (Mid(TextLine, quoteLoop, 1) <> quote)
                        quoteLoop = quoteLoop - 1
                    End While
                    htmFile = Mid(TextLine, quoteLoop + 1, endQuote - quoteLoop - 1)
                    If (Len(Dir(dirfile & htmFile)) = 0) Then
                        htmFile = dirfile & "\" & htmFile
                    Else
                        htmFile = dirfile & htmFile
                    End If
                    Dim b As Boolean = False
                    Try
                        b = File.Exists(htmFile)
                    Catch ex As Exception

                    End Try
                    If (Not b) Then
                        Continue For
                    End If
                    wApp.Selection.InsertParagraphAfter()
                    filenumber += 1
                    RaiseEvent ProcessFile(Me, New ProcessFileEventArgs(htmFile, filenumber))
                    wApp.Selection.InsertFile(htmFile, ConfirmConversions:=False, Link:=True)
                    If (filenumber Mod 10 = 0) Then wDoc.Save()
                End If
            Next TextLine
            wDoc.Save()
            wDoc.Close()
            wApp.Quit()
        Catch ex As Exception

        Finally
            If (wDoc IsNot Nothing) Then
                Marshal.ReleaseComObject(wDoc)
                wDoc = Nothing
            End If
            Marshal.ReleaseComObject(wApp)
            wApp = Nothing

        End Try

    End Sub
End Class
